/**
 * AgaEngine (c)2011.
 * FileName:Rectangle.java
 */
package aga.engine.physic;

import aga.engine.AgaCfg;
import android.graphics.*;
import android.graphics.Paint.Style;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

/**
 * 
 * @author User
 */
public class Circle extends PhysicBody {
	//===========================================================
	//Constants
	//===========================================================

	//===========================================================
	//Fields
	//===========================================================

	public int color = 0;

	private Paint paint = new Paint();
	private Paint paintBorder = new Paint();
	private Matrix matrix = new Matrix();

	//===========================================================
	//Constructors
	//===========================================================

	protected Circle(World world, float x, float y, float radius, float density, float friction,
                     float restitution, BodyType bt, int color) {
        super(world);

		this.color = color;
		if (this.color != 0) {
			paint.setColor(color);
			paint.setAlpha(50);

			paintBorder.setColor(color);
			paintBorder.setStyle(Style.STROKE);
		} else if (bt == BodyType.DYNAMIC) {
			paint.setColor(Color.GREEN);
			paint.setAlpha(50);

			paintBorder.setColor(Color.GREEN);
			paintBorder.setStyle(Style.STROKE);
			paintBorder.setAntiAlias(true);
		} else {
			paint.setColor(Color.BLUE);
			paint.setAlpha(50);

			paintBorder.setColor(Color.BLUE);
			paintBorder.setStyle(Style.STROKE);
		}

		
		CircleShape circleDef = new CircleShape();
		circleDef.m_p.set(x, y);
        circleDef.m_radius = radius;
		
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = circleDef;
		fixtureDef.density = density;
		fixtureDef.friction = friction;
		fixtureDef.restitution = restitution;

		bodyDef.position.x = x;
		bodyDef.position.y = y;
		bodyDef.type = bt;
		body = world.createBody(bodyDef);
		body.createFixture(fixtureDef);


	}


	//===========================================================
	//Getter & Setter
	//===========================================================
	
	

	
	//===========================================================
	//Methods for/from SuperClass/Interfaces
	//===========================================================
	
	
	@Override
	public void draw(Canvas canvas) {
		
		canvas.save();
		
		matrix.reset();
		matrix.preRotate(getAngle(), getX()*AgaCfg.PHY2MAP_SCALE, getY()*AgaCfg.PHY2MAP_SCALE);
		canvas.setMatrix(matrix);
		canvas.drawCircle(getX()*AgaCfg.PHY2MAP_SCALE, getY()*AgaCfg.PHY2MAP_SCALE,
                body.getFixtureList().getNext().getShape().m_radius, paint);
		canvas.drawCircle(getX()*AgaCfg.PHY2MAP_SCALE, getY()*AgaCfg.PHY2MAP_SCALE,
                body.getFixtureList().getNext().getShape().m_radius, paintBorder);
		
		canvas.restore();
	}

	//===========================================================
	//Methods
	//===========================================================

	//===========================================================
	//Inner and Anonymous Classes
	//===========================================================

    @Override
    public void destroy() {
        mWorld.destroyBody(body);
    }
}
